#discerner_search
  = render 'discerner/searches/summary'
  .discerner-buttons
    = link_to 'Back to search', edit_search_path(@discerner_search), class: "discerner-button discerner-button-positive"
  .clear
    %hr

  - unless @discerner_search.disabled? && @discerner_search.export_parameters && @discerner_search.export_parameters.select{|ep| ep.disabled?}.empty?
    #discerner_exportable_fields
      %h3
        Fields to be exported

      - exportable_parameter_categories = exportable_parameter_categories(@discerner_search)
      - if exportable_parameter_categories.blank?
        = "-- No exportable parameters detected --"
      - else
        - selected_parameters = @discerner_search.export_parameters.ordered.to_a
        - selected_parameters = @discerner_search.search_parameters.ordered.to_a + @discerner_search.nested_searches.map{|s| s.search_parameters}.flatten if selected_parameters.empty?

        = form_tag assign_export_parameters_path(@discerner_search), url: {action: "assign"} do
          - exportable_parameter_categories.each do |c|
            .clear
            %div{ class: "#{c.css_class_name}"}
              %h4
                = c.name
                %a{ class: "select_all_parameters", href: "#"}
                  Select all

              - items = exportable_parameters(@discerner_search, c).reject{|p| p.hidden_in_export}
              - columns_count = 3
              - items_rendered_count = 0
              - (0...columns_count).each do |i|
                .column
                  - column_items_count = ((items.size - items_rendered_count).to_f/(columns_count-i)).ceil
                  - from  = items_rendered_count
                  - to    = items_rendered_count + column_items_count
                  - to    = items.size if to > items.size
                  - items_rendered_count = to
                  - (from...to).each do |j|
                    - export_parameter = items[j]
                    %div{ class: "export_parameter #{'error' if export_parameter.deleted?}"}
                      - is_checked = selected_parameters.select{ |cp| cp.parameter_id == export_parameter.id }.blank? ? false : true
                      = check_box_tag "parameter_ids[]", export_parameter.id, is_checked, id: "parameter_ids_#{export_parameter.id}"
                      = label_tag "parameter_ids_#{export_parameter.id}", export_parameter.name

          .clear
          .discerner-buttons
            - if @discerner_search.disabled?
              = submit_tag 'Update and export', class: "discerner-button discerner-button-positive"
            - else
              = submit_tag 'Export', class: "discerner-button discerner-button-positive"
:javascript
  $(function () {
    new Discerner.ExportParameters.UI();
  });